package com.hbm.usercenter.fragment;


import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.hbm.usercenter.MainActivity;
import com.hbm.usercenter.R;
import com.hbm.usercenter.base.BaseFragment;
import com.hbm.usercenter.base.LoadingPager.LoadedResult;
import com.hbm.usercenter.conf.Constants;
import com.hbm.usercenter.http.MyCallBack;
import com.hbm.usercenter.http.NetClient;
import com.hbm.usercenter.utils.CommonUtils;
import com.hbm.usercenter.utils.LogUtils;
import com.hbm.usercenter.utils.UIUtils;
import com.hbm.usercenter.widget.GroupInfoEye;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class UpdatePwdFragment extends BaseFragment {

    @Bind(R.id.et_pwd)
    EditText mEtPwd;
    @Bind(R.id.et_pwd_new)
    GroupInfoEye mEtPwdNew;
    @Bind(R.id.et_pwd_confirm)
    GroupInfoEye mEtPwdConfirm;
    @Bind(R.id.btn_update)
    Button mBtnUpdate;

    @Override
    protected LoadedResult initData() {
        return LoadedResult.SUCCESS;
    }

    @Override
    protected View initSuccessView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.fragment_update_pwd, null);
        ButterKnife.bind(this, view);
        return view;

    }

    @OnClick(R.id.btn_update)
    public void onViewClicked() {
        sendRequest();
    }

    private void sendRequest() {
        String oldPwd = CommonUtils.getEtText(mEtPwd);
        String newPwd = mEtPwdNew.getPwd();
        String rpPwd = mEtPwdConfirm.getPwd();
        Map<String, String> params = new HashMap<String, String>();
        params.put("old_pw", oldPwd);
        params.put("new_pw", newPwd);
        params.put("re_pw", rpPwd);
        NetClient.getNetClient().callNet(Constants.URLS.UPDATEPWD, new MyCallBack() {
            @Override
            public void onFailure(IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(String json) {
                processResult(json);
                LogUtils.d(json);

            }
        }, params);

    }

    private void processResult(String json) {
        JSONObject rootObject = null;
        try {
            rootObject = new JSONObject(json);
            final String err_msg = rootObject.getString("err_msg");
            String err_code = rootObject.getString("err_code");
            if (err_code.equals("-1")) {
                UIUtils.postTaskSafely(new Runnable() {
                    @Override
                    public void run() {
                        CommonUtils.showShort(UIUtils.getContext(), err_msg);
                    }
                });
                //                Intent intent = new Intent(getActivity(), QRCodeActivity.class);
                //                intent.putExtra("money", number);
                //                //                intent.putExtra("money", money);
                //                //                intent.putExtra("picUrl", picUrl);
                //                //
                //                startActivity(intent);
                startActivity(new Intent(getActivity(), MainActivity.class));
            } else {
                UIUtils.postTaskSafely(new Runnable() {
                    @Override
                    public void run() {
                        CommonUtils.showShort(UIUtils.getContext(), err_msg);
                    }
                });
                //                Toast.makeText(UIUtils.getContext(), err_msg, Toast
                // .LENGTH_SHORT).show();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}
